﻿#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
/*int int_cmp(const void* p1, const void* p2)
{
	return *(int*)p1 - *(int*)p2;
}*/
struct Stu
{
	char name[20];
	int age;
};
int name_cmp(const void* p1, const void* p2)
{
	return *(char*)p1 - *(char*)p2;
}
void Swap(char*buf1,char*buf2,size_t wideth)
{
	for (int i = 1; i <= wideth; i++)
	{
		char tmp = *buf1;
		*buf1 = *buf2;
		*buf2 = tmp;
		buf1++;
		buf2++;
	}
}
void bubble_sort(void* base, size_t sz, size_t wideth, int (*cmp)(void* p1, void* p2))
{
	for (int i = 0; i < sz; i++)
	{
		for (int j = 0; j < sz - 1 - i; j++)
		{
			if (cmp((char*)base+j*wideth,(char*)base+(j+1)*wideth)>0)
			{
				Swap((char*)base+j*wideth,(char*)base+(j+1)*wideth,wideth);
			}
		}
	}
}
void print(int arr[],int sz)
{
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
}
int main()
{
	//int arr[10] = { 9,8,7,6,5,4,3,2,1,0 };
	struct Stu arr[3] = { {"zhangshan",20},{"lishi",19},{"wangwu",18} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	bubble_sort(arr, sz,sizeof(arr[0]),name_cmp);
	print(arr, sz);
	return 0;
}